import Router from 'vue-router'
import Vue from 'vue'
import GlobalRouter from '@/router/modules/global'
import * as DashBoardRouters from '@/router/modules/dashboard'
import * as UserRouters from '@/router/modules/user'
import * as DataRouters from '@/router/modules/data'

//import store from "@/store"

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)

let routes = [
	{
		path: '/home',
		component: () => import('@/views/Home/Home'),
		name: 'home',
		meta: {title: '主入口整体布局'},
		children: []
	}
]
// 将多个导出的对象合并成一个对象
let childrenRouters = Object.assign(DashBoardRouters, UserRouters, DataRouters)
// 将childrenRouters里面的数据全部放到routes[0].children数组里面
Object.keys(childrenRouters).forEach(key => {
	routes[0].children.push(childrenRouters[key])
})

routes = [...GlobalRouter, ...routes]
let router = new Router({
	// hash模式是默认的模式,如果配置history模式,需要在配置相关nginx
	mode: 'history',
	scrollBehavior: () => ({y: 0}),
	// isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
	// 将两个路由链接到一起的函数
	routes: routes
})

//权限判断
router.beforeEach((to, from, next) => {
	next()
})

// 路由添加
/*router.afterEach((enter, leave) => {
  // 离开的标签需要缓存
  if (leave.meta.keepAlive) {
    // 将该组件下的tabInfo放入到tabs中
    store.dispatch('settings/addTab', leave.meta.tab).then().catch()
    // 将该组件的名称(就是name:'xxx')
    store.dispatch('settings/addKeepAlive', leave.meta.tab.componentName).then().catch()
  }
})*/

export default router

